package com.example.controller;

import com.example.constants.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author wen
 * @date 2021/8/17
 */
@RestController
@RequestMapping("length")
public class TestMaxLengthController {
    @Resource
    private RabbitTemplate rabbitTemplate;


    @RequestMapping("test/{length}")
    public String send(@PathVariable("length") Integer maxLength) {

        /**
         * 发送消息
         */
        for (int i = 0; i < maxLength; i++) {
            String str = "我是第 " + i + " 条消息\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss_SSS").format(new Date());
            rabbitTemplate.convertAndSend(Constants.MY_LENGTH_FANOUT_EXCHANGE, "", str);
            System.out.println("消息发送成功!\t" + str);
        }
        return "success";
    }
}
